﻿using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using SharpDX;
using SharpDX.Multimedia;
using SharpDX.XAudio2;
using ScheduledXAudio2.Structures;

namespace ScheduledXAudio2
{

    /// <summary>
    /// Tracks and provides neccesary objects for a DrumPatchInstance.
    /// (Formerly "Sound")
    /// </summary>
    public class DrumPatchHost : PatchHost
    {
        public DrumPatchHost()
        {
            Clock = ScheduledXAudio2.XA2IOFront.Scheduler;
        }
        public DrumPatchHost(string path)
            : base(path)
        {            
            Clock = ScheduledXAudio2.XA2IOFront.Scheduler;            
        }
        public DrumPatchHost(System.IO.Stream stream):base(stream)
        {
            Clock = ScheduledXAudio2.XA2IOFront.Scheduler;
        }

        #region Properties
        public bool HostIsTerminal
        {
            get;
            protected set;
        }
        #endregion

        #region Public Methods
        public bool SendDrumNote(VoiceSendDescriptor _vsd, WaveFormat __format, AudioBuffer _buffer)
        {
            DrumPatchInstance instance = new DrumPatchInstance(_vsd, __format, _buffer);
            XA2IOFront.AddInstance(instance);
            //_GraphingEffect.IsPlaying = true;

            return true;
        }
        public bool SendDrumNote(Structures.SimpleUnit unit)
        {
            DrumPatchInstance instance = new DrumPatchInstance(unit);
            XA2IOFront.AddInstance(instance);

            return true;
        }
        /// <summary>
        /// Load a file from a path
        /// </summary>
        /// <param name="soundName"></param>
        /// <returns></returns>
        public override bool Load(string soundName)
        {
            if (DrumPatchLoader.IsLoading)
            {
                //the intention here is that if the Loader is loading this 
                //method loops until the loader is not loading.
                Load(soundName);                
            }
            else
            {
                if (DrumPatchLoader.LoadWAV(out Unit, soundName))
                {
                    Unit.SetOutputVoice(sound_submixvoice);
                    return true;
                }
                else
                    return false;
            }
            return false;
        }
        /// <summary>
        /// Load a stream
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public override bool Load(System.IO.Stream stream)
        {
            if (DrumPatchLoader.IsLoading)
            {
                Load(stream);
            }
            else
            {
                if (DrumPatchLoader.LoadStream(out Unit, stream))
                {
                    Unit.SetOutputVoice(sound_submixvoice);
                    return true;
                }
                else
                    return false;
            }
            return false;
        }
        public override void Unload()
        {            
            if (UNIT != null)
                DrumPatchLoader.UnloadWav(UNIT);
        }
        /// <summary>
        /// Loads a sound from a file.
        /// </summary>
        /// <param name="soundName">The path to the sound file</param>
        /// <returns>Whether the sound loaded successfully</returns>
        
       
        #endregion

        #region IDisposable

        public override void Dispose()
        {
            HostIsTerminal = true;
            Unload();
            //graphingEffect.Dispose();
        }

        #endregion

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}